Return to start page
Core/Debug/Struct Cheat.j
1 /// @author Tamino Dauth
2 library AStructCoreDebugCheat requires ALibraryCoreDebugMisc, AStructCoreGeneralHashTable, ALibraryCoreGeneralPlayer
3
4 /// @todo Should be a part of @struct ACheat, vJass bug.
5 function interface ACheatOnCheatAction takes nothing returns nothing
6
7 /**
8 * ACheat provides a simple cheat functionality. Cheats are string which the player has to enter in chat and which can provide everything the user wants them.
9 * Use the function interface @functioninterface ACheatOnCheatAction to specify your cheat action.
10 * Note that you can use @function GetEventPlayerChatString() to read the whole entered chat string.
11 */
12 struct ACheat
13 //start members
14 private string m_cheat
15 private boolean m_exactMatch
16 private ACheatOnCheatAction m_action
17 //members
18 private trigger cheatTrigger
19
20 //! runtextmacro optional A_STRUCT_DEBUG("\"ACheat\"")
21
22 //start members
23
24 public method cheat takes nothing returns string
25 return this.m_cheat
26 endmethod
27
28 public method exactMatch takes nothing returns boolean
29 return this.m_exactMatch
30 endmethod
31
32 //methods
33
34 private static method triggerActionCheat takes nothing returns nothing
35 local trigger triggeringTrigger = GetTriggeringTrigger()
36 local ACheat this = AHashTable.global().handleInteger(triggeringTrigger, "this")
37 call this.m_action.execute()
38 set triggeringTrigger = null
39 endmethod
40
41 private method createCheatTrigger takes nothing returns nothing
42 local integer i
43 local player user
44 local event triggerEvent
45 local triggeraction triggerAction
46 set this.cheatTrigger = CreateTrigger()
47 set i = 0
48 loop
49 exitwhen (i == bj_MAX_PLAYERS)
50 set user = Player(i)
51 if (IsPlayerPlayingUser(user)) then
52 set triggerEvent = TriggerRegisterPlayerChatEvent(this.cheatTrigger, user, this.m_cheat, this.m_exactMatch)
53 set triggerEvent = null
54 endif
55 set user = null
56 set i = i + 1
57 endloop
58 set triggerAction = TriggerAddAction(this.cheatTrigger, function ACheat.triggerActionCheat)
59 set triggerAction = null
60 call AHashTable.global().setHandleInteger(this.cheatTrigger, "this", this)
61 endmethod
62
63 /**
64 * @param cheat The string the player has to enter into the chat.
65 * @param exactMatch If this value is false user does not have to enter the exact string of @param cheat to run the cheat. For example if the cheat string is "setlevel" "setlevel 1000" does also work.
66 * @param action The function which will be called when player enters cheat string.
67 */
68 public static method create takes string cheat, boolean exactMatch, ACheatOnCheatAction action returns ACheat
69 local ACheat this = ACheat.allocate()
70 debug if (cheat == null) then
71 debug call this.print("cheat is empty.")
72 debug endif
73 debug if (action == 0) then
74 debug call this.print("action is 0.")
75 debug endif
76 //start members
77 set this.m_cheat = cheat
78 set this.m_exactMatch = exactMatch
79 set this.m_action = action
80
81 call this.createCheatTrigger()
82 return this
83 endmethod
84
85 public method onDestroy takes nothing returns nothing
86 call AHashTable.global().destroyTrigger(this.cheatTrigger)
87 set this.cheatTrigger = null
88 endmethod
89 endstruct
90
91 endlibrary